package com.bbzn.device.client.utils;

import org.apache.commons.codec.binary.Hex;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author wulongbo
 * @Date 2021/11/8 19:00
 * @Version 1.0
 */
public class Base64Util {

    private final static Base64.Decoder decoder = Base64.getDecoder();

    private final static Base64.Encoder encoder = Base64.getEncoder();

    /**
     * @param str
     * @return
     * @description 对字符串进行base64编码
     */
    public static String decode(String str) {
        final byte[] strByte = str.getBytes();
        return encoder.encodeToString(strByte);
    }

    private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    public static final String toHex(String code) {
        byte[] data = Base64.getDecoder().decode(code);
        final StringBuffer sb = new StringBuffer(data.length * 2);
        for (int i = 0; i < data.length; i++) {
            sb.append(DIGITS[(data[i] >>> 4) & 0x0F]);
            sb.append(DIGITS[data[i] & 0x0F]);
        }
        return sb.toString().toLowerCase();
    }

    /**
     * @param str
     * @return
     * @description 对字符串进行base64解码
     */
    public static String encode(String str) {
        return new String(decoder.decode(str));
    }


//    public static void main(String[] args) {
//////        String str = "eyJjb2RlIjogIjMwMDEiLCJhc2tUaW1lIjoiMTYwMDY1NTE0NzI3MSIsImltZWkiOiI5OTAwMTAyNjMyOTIzNyJ9";
//////        String decodeStr = Base64Util.decode(str);	// aGVsbG8gd29ybGQ=
//////        System.out.println(decodeStr);
//////        System.out.println(Base64Util.encode(str));
////        String decodeStr=Base64Util.decode("9920999999"+"::"+12);
////        System.out.println(decodeStr);
////        System.out.println(Base64Util.encode(str));
//        String decodeStr=Base64Util.decode("9920999999"+"::"+12);
//        System.out.println(decodeStr);
//        System.out.println(Base64Util.encode(str));
//        String decodeStr = Base64Util.decode("9920999999" + "::" + 12);
//        System.out.println(decodeStr);
//        System.out.println(Base64Util.decode("案例看手机发廊林"));
//
//        String code = "qt0LIADFCGhRECAAACkAvAARAAwtAAAAAAAAAB5wC1U=";
//        String data = toHex(code);
//        System.out.println(data);
//        String state = data.substring(10, 12);
//        int b = HexUtil.hex16To10(state);
//        String imei = data.substring(13, 28);
//        String bat = data.substring(56, 58);
//        System.out.println(imei);
//        System.out.println(bat);
//    }

}
